package com.sand.controller;

import com.alibaba.fastjson.JSON;
import com.sand.entity.CityEntity;
import com.sand.entity.InformationEntity;
import com.sand.service.IAuthService;
import com.sand.service.ICityService;
import com.sand.util.ModelTest;
import com.sand.util.PageModel;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping(value = "city")
public class CityController {
/**
 *
 *
 * @author <achao1441470436@gmail.com>
 * @since 2021/12/3/003 16:33
 */

    //authService接口
    @Resource(name = "cityService")
    private ICityService cityService;

    //分页查询城市
    @RequestMapping(value = "/queryCityLimit")
    public void queryCityLimit(int limit, int page, String condition, HttpServletRequest request, HttpServletResponse response)throws Exception{
        response.setCharacterEncoding("utf-8");

        PageModel<HashMap<String,Object>> pm = cityService.queryCityLimit(limit, page, condition);
        ModelTest<List<HashMap<String,Object>>> listData = new ModelTest<List<HashMap<String,Object>>>("OK",pm.getList(),0, pm.getPagecount());
        response.getWriter().write(JSON.toJSON(listData).toString());
    }
    //删除资讯
    @RequestMapping(value = "/delCommunity")
    public void delCommunity(String data,HttpServletResponse response)throws Exception{
        boolean bln = cityService.delCommunity(data);
        if(bln){
            response.getWriter().write("OK");
        }else{
            response.getWriter().write("NO");
        }
    }
    //添加资讯
    @RequestMapping(value = "/addCommunity")
    public void addCommunity(String data,HttpServletResponse response)throws Exception{
        boolean bln = cityService.addCommunity(data);
        if(bln){
            response.getWriter().write("OK");
        }else{
            response.getWriter().write("NO");
        }
    }
    //根据id查询资讯
    @RequestMapping(value = "/queryUserById")
    public ModelAndView queryUserById(int rid){
        return new ModelAndView("layui/page/table/edit_city").addObject("upCity",cityService.queryCommunityById(rid));
    }
    //修改资讯
    @RequestMapping(value = "/updateCommunity",method = RequestMethod.POST)
    public void updateCommunity(String data,HttpServletResponse response)throws Exception{
        boolean bln = cityService.updateCommunity(data);
        if(bln){
            response.getWriter().write("OK");
        }else{
            response.getWriter().write("NO");
        }
    }
    //根据城市名查询id
    @RequestMapping(value = "/queryCityByName")
    public void queryCityByName(String cityName, HttpServletRequest request,HttpServletResponse response)throws Exception{
        response.setCharacterEncoding("utf-8");
        if(cityName!=null){
            if(cityName.indexOf("省")>0){
                int index = cityName.indexOf("市");
                cityName=cityName.substring(cityName.indexOf("省")+1,index);
            }
        }

        CityEntity city = cityService.queryCityByName(cityName);
        request.getSession().setAttribute("city",city);
        response.getWriter().write(JSON.toJSON(city).toString());
    }
    //城市选择查询
    @RequestMapping(value = "/queryCityList")
    public void queryCityList(int rid,int showWi,String condition,HttpServletResponse response)throws Exception{
        response.setCharacterEncoding("utf-8");
        System.out.println("=====>访问了queryCityList");
        HashMap<String, Object> map = cityService.queryCityList(rid, showWi,condition);
        response.getWriter().write(JSON.toJSON(map).toString());
    }

    //查询所有城市
    @RequestMapping(value = "/queryAllCity")
    public ModelAndView queryAllCity(){
        return new ModelAndView("renting/pages/onther/table_city_show").addObject("cityShow",cityService.queryAllCity());
    }

    //根据城市名模糊查询
    @RequestMapping(value = "/queryCityByLikeName")
    public void queryCityByLikeName(String name,HttpServletResponse response)throws Exception{
        response.setCharacterEncoding("utf-8");

        List<CityEntity> cityList = cityService.queryCityByLikeName(name);
        response.getWriter().write(JSON.toJSON(cityList).toString());
    }

    //根据小区名模糊查询
    @RequestMapping(value = "/queryComByLikeName")
    public void queryComByLikeName(String condition,HttpServletResponse response)throws Exception{
        response.setCharacterEncoding("utf-8");

        List<HashMap<String,Object>> cityList = cityService.queryComByLikeName(condition);
        response.getWriter().write(JSON.toJSON(cityList).toString());
    }



}
